package com.woparker.kiosk.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.woparker.kiosk.R;
import com.woparker.kiosk.adapter.LotsAdapter;
import com.woparker.kiosk.base.BaseActivity;
import com.woparker.kiosk.bean.SlotDTO;
import com.woparker.kiosk.view.EmptyDataLayout;
import com.woparker.kiosk.view.GridDividerDecoration;
import com.woparker.kiosk.view.TimeKey;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;


public class SearchResultsActivity extends BaseActivity {

    private View rootView;

    private EmptyDataLayout mEmptyDataLayout;

    private GridDividerDecoration mGridDivider;

    public LotsAdapter lotsAdapter;
    private List<SlotDTO> itemList = new ArrayList();

    private RecyclerView parkGrid;

    private EmptyDataLayout emptyDataLayout;

    private Button return_but;

    private Button enter_time;

    @Override
    public View createView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_search_result, null);
        return view;
    }

    @Override
    public void initView() {
        rootView = findViewById(R.id.root_view);
        parkGrid = findViewById(R.id.parkGrid);
        mEmptyDataLayout = findViewById(R.id.emptyDataLayout);
        mGridDivider = new GridDividerDecoration();
        parkGrid.setLayoutManager(new GridLayoutManager(this, 4));
        parkGrid.setItemAnimator(new DefaultItemAnimator());
        mGridDivider.setDrawHeaderDivider(true);
        mGridDivider.setDrawFooterDivider(true);
        parkGrid.addItemDecoration(mGridDivider);
        return_but = findViewById(R.id.return_but);
        enter_time = findViewById(R.id.enter_time);
        emptyDataLayout = findViewById(R.id.emptyDataLayout);
        emptyDataLayout.setReinputClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        emptyDataLayout.setRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return_but.setOnClickListener(view -> {
            this.finish();
        });
        enter_time.setOnClickListener(view -> {
            showBottomPopupWindow(rootView);
        });
    }

    @Override
    public void initData() {
        itemList = new ArrayList();
        itemList.add(new SlotDTO(1, "1号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A88888", "黑色", "https://img1.baidu.com/it/u=2999105660,3307073421&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(2, "2号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A55555", "黑色", "https://img0.baidu.com/it/u=867575291,1578087018&fm=253&fmt=auto&app=120&f=JPEG?w=667&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(3, "3号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A66666", "黑色", "https://img2.baidu.com/it/u=2367866501,2930830059&fm=253&fmt=auto&app=120&f=JPEG?w=667&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(4, "4号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A77777", "黑色", "https://img2.baidu.com/it/u=3048626074,2455532863&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(5, "5号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A99999", "黑色", "https://img0.baidu.com/it/u=2711395635,1571711264&fm=253&fmt=auto&app=138&f=JPEG?w=833&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(6, "6号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A23453", "黑色", "https://img1.baidu.com/it/u=2416758852,3159115842&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(7, "7号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A66545", "黑色", "https://img2.baidu.com/it/u=628070123,4075793524&fm=253&fmt=auto&app=120&f=JPEG?w=752&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(8, "8号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A887665", "黑色", "https://img2.baidu.com/it/u=2050373218,4122385341&fm=253&fmt=auto&app=120&f=JPEG?w=707&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(9, "9号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A223433", "黑色", "https://img2.baidu.com/it/u=3173789731,2372005021&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=433", 652, 328, "1号场"));
        itemList.add(new SlotDTO(10, "10号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A11234", "黑色", "https://img1.baidu.com/it/u=3807356453,455824619&fm=253&fmt=auto&app=120&f=JPEG?w=654&h=399", 652, 328, "1号场"));
        itemList.add(new SlotDTO(11, "11号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A223433", "黑色", "https://img2.baidu.com/it/u=3173789731,2372005021&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=433", 652, 328, "1号场"));
        itemList.add(new SlotDTO(12, "12号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A54332", "黑色", "https://img1.baidu.com/it/u=4046693671,1007821944&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(13, "13号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A554333", "黑色", "https://img0.baidu.com/it/u=2080986506,1827822727&fm=253&fmt=auto&app=120&f=JPEG?w=835&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(14, "14号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A22344", "黑色", "https://img0.baidu.com/it/u=4055060110,4193616569&fm=253&fmt=auto&app=138&f=JPEG?w=1221&h=500", 652, 328, "1号场"));
        itemList.add(new SlotDTO(15, "15号车位", "1号场", "1号区域", System.currentTimeMillis(), "粤A66775", "黑色", "https://img2.baidu.com/it/u=3436209483,295104682&fm=253&fmt=auto&app=120&f=JPEG?w=695&h=500", 652, 328, "1号场"));


        if (!itemList.isEmpty()) {
            parkGrid.setVisibility(View.VISIBLE);
            mEmptyDataLayout.hide();
            setMyAdapter();
        } else {
            parkGrid.setVisibility(View.GONE);
            mEmptyDataLayout.show();
        }
    }

    public void setMyAdapter() {
        if (null == lotsAdapter) {
            lotsAdapter = new LotsAdapter(this, itemList);
            parkGrid.postDelayed(new Runnable() {
                @Override
                public void run() {
                    parkGrid.setAdapter(lotsAdapter);
                }
            }, 200);
        } else {
            lotsAdapter.notifyDataSetChanged();
        }
    }


    private void showBottomPopupWindow(View anchorView) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_bottom, null);
        int width = LinearLayout.LayoutParams.MATCH_PARENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true;
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.showAtLocation(anchorView, Gravity.BOTTOM, 0, 0);
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
        TimeKey timeKey = (TimeKey) popupView.findViewById(R.id.key_time);
        timeKey.showInfo();
        timeKey.setSearchListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String starttime = timeKey.getStarttiem();
                String endtime = timeKey.getEndtime();
                Log.e("TAG", "starttime:" + starttime + " endtime:" + endtime);
                popupWindow.dismiss();
            }
        });
    }
}
